﻿const query = require('../client');
const moment = require('moment');
const createGuid = require('../lib/util').createGuid;
module.exports = {
    // 获取通知列表
    getNotices(classGuid, n, m, state, callback) {
        //PublicObject
        let sql = `SELECT n.PK_NoticeGuid,n.Title,n.Content,n.State,n.PublicTime ,n.Num 
        FROM notice as n 
        WHERE FK_ClassGuid='${classGuid}' and State='${state}' ORDER BY PublicTime DESC 
        limit ${n},${m}
        ;`;
        query(sql, function(err, vals) {
            if (err) {
                console.log(err);
                return;
            }
            callback(vals);
        });
    },
    // 添加通知
    addNotice(data, callback) {
        let noticeGuid = createGuid();
        let title = data.title;
        let content = data.content;
        // let object = JSON.stringify(data.object);
        let createTime = moment().format('YYYY-MM-DD HH:mm:ss').toString();
        let classGuid = data.classGuid;
        let state = 0;
        let sql = `INSERT INTO notice(PK_NoticeGuid,Title,Content,PublicTime,FK_ClassGuid,State,Num) VALUES(
              '${noticeGuid}','${title}','${content}','${createTime}','${classGuid}',${state},${data.object.length});`;
        console.log(sql)
        console.log(data)
        query(sql, function(err, vals) {
            if (err) {
                console.log(err);
                return;
            }
            let sql0 = `insert into lessonplan_user.user_in_notice(PK_UINGuid,FK_UserGuid,FK_NoticeGuid) values`
            for (let i = 0; i < data.object.length; i++) {
                sql0 += `('${createGuid()}','${data.object[i].PK_UserGuid}','${noticeGuid}'),`
            }
            sql0 = sql0.substring(0, sql0.lastIndexOf(","));
            query(sql0, function(err0, vals0) {
                if (err0) {
                    console.log(err0);
                    return;
                }
                callback(vals);
            })
        });
    },
    // 修改通知
    updateNotice(data, callback) {
        let noticeGuid = data.noticeGuid;
        let title = data.title;
        let content = data.content;
        let object = data.object;
        let sql = `UPDATE notice SET Title='${title}',Content='${content}',Num=${data.object.length}  WHERE PK_NoticeGuid='${noticeGuid}';`;
        query(sql, function(err, vals) {
            if (err) {
                console.log(err);
                return;
            }
            let sql1 = `DELETE FROM lessonplan_user.user_in_notice where lessonplan_user.user_in_notice.FK_NoticeGuid='${noticeGuid}' `
            query(sql1, function(err1, vals1) {
                if (err1) {
                    console.log(err1);
                    return;
                }
                let sql0 = `insert into lessonplan_user.user_in_notice(PK_UINGuid,FK_UserGuid,FK_NoticeGuid) values`
                for (let i = 0; i < data.object.length; i++) {
                    sql0 += `('${createGuid()}','${data.object[i].PK_UserGuid}','${noticeGuid}'),`
                }
                sql0 = sql0.substring(0, sql0.lastIndexOf(","));
                query(sql0, function(err0, vals0) {
                    if (err0) {
                        console.log(err0);
                        return;
                    }
                    callback(vals);
                })
            })
        });
    },
    // 删除通知
    delNotice(guid, callback) {
        let sql = `UPDATE notice SET State=99 WHERE PK_NoticeGuid='${guid}';`;
        query(sql, function(err, vals) {
            if (err) {
                console.log(err);
                return;
            }
            callback(vals);
        });
    },
    // 恢复通知
    recoverNotice(guid, callback) {
        let sql = `UPDATE notice SET State=0 WHERE PK_NoticeGuid='${guid}';`;
        query(sql, function(err, vals) {
            if (err) {
                console.log(err);
                return;
            }
            callback(vals);
        });
    },
    read(data, callback) {
        let sql = `UPDATE lessonplan_user.user_in_notice set lessonplan_user.user_in_notice.HaveRead=1 where lessonplan_user.user_in_notice.FK_UserGuid='${data.FK_UserGuid}' and lessonplan_user.user_in_notice.FK_NoticeGuid ='${data.FK_NoticeGuid}'`
        console.log(sql)
        query(sql, function(err, vals) {
            if (err) {
                console.log(err);
                return;
            }
            callback(vals);
        });
    },
    getNoticeByUser(data, callback) {
        let callbackdata = []
        let sql1 = `select lessonplan_user.user_in_notice.FK_NoticeGuid,lessonplan_user.user_in_notice.HaveRead from lessonplan_user.user_in_notice where lessonplan_user.user_in_notice.FK_UserGuid='${data.FK_UserGuid}'`
        query(sql1, function(err1, vals1) {
            if (err1) {
                console.log(err1);
                callback({ status: 0, err: '服务器错误' })
                return;

            }
            let array1 = vals1;

            if (array1.length > 0) {
                let noticeguidlist = ""
                for (let i = 0; i < array1.length; i++) {
                    noticeguidlist += `"${array1[i].FK_NoticeGuid}",`
                }
                noticeguidlist = noticeguidlist.substring(0, noticeguidlist.lastIndexOf(","));
                let sql = `SELECT PK_NoticeGuid,Title,Content,State,PublicTime ,Num 
                FROM notice  
                WHERE FK_ClassGuid='${data.FK_ClassGuid}' and State='${data.state}' and PK_NoticeGuid IN(${noticeguidlist}) `
                query(sql, function(err, vals) {
                    if (vals.length > 0) {
                        for (let i = 0; i < array1.length; i++) {
                            for (let j = 0; j < vals.length; j++) {
                                if (array1[i].FK_NoticeGuid == vals[j].PK_NoticeGuid) vals[j].HaveRead = array1[i].HaveRead
                            }
                        }
                        callback({ status: 1, data: vals })
                    } else callback({ status: 1, data: [] })
                })

            } else callback({ status: 1, data: [] })
        })
    },
    Unread(data, callback) {
        let flag = false;
        let sql1 = `select lessonplan_user.user_in_notice.FK_NoticeGuid,lessonplan_user.user_in_notice.HaveRead from lessonplan_user.user_in_notice where lessonplan_user.user_in_notice.FK_UserGuid='${data.FK_UserGuid}'`
        query(sql1, function(err1, vals1) {

            let array1 = vals1;
            if (array1.length > 0) {
                let noticeguidlist = ""
                for (let i = 0; i < array1.length; i++) {
                    noticeguidlist += `"${array1[i].FK_NoticeGuid}",`
                }
                noticeguidlist = noticeguidlist.substring(0, noticeguidlist.lastIndexOf(","));
                let sql = `SELECT PK_NoticeGuid,Title,Content,State,PublicTime ,Num 
                FROM notice  
                WHERE FK_ClassGuid='${data.FK_ClassGuid}' and State='${data.state}' and PK_NoticeGuid IN(${noticeguidlist}) `
                query(sql, function(err, vals) {
                    if (vals.length > 0) {
                        for (let i = 0; i < array1.length; i++) {
                            for (let j = 0; j < vals.length; j++) {
                                if (array1[i].FK_NoticeGuid == vals[j].PK_NoticeGuid) {
                                    if (array1[i].HaveRead == 0) {
                                        flag = true;
                                    }
                                }
                            }
                        }
                        callback({ status: 1, haveUnread: flag })
                    } else callback({ status: 1, data: [] })
                })
            } else callback({ status: 1, data: [] })
        })
    }
};